Skip to main content

Bigint

Must be included via require.


bigint.new

To instantiate a bigint, you can call the bigint.new function, or use the new operator.

Parameters

  1. The decimal value of the bigint.
pluto
local bigint = require "pluto:bigint"
print(new bigint("123")) --> 123

bigint.tostring, __tostring

As seen above, the __tostring metamethod provides a string representation of the Bigint in decimal.


bigint.hex

Provides a hexadecimal representation of the Bigint.

pluto
local bigint = require "pluto:bigint"
print(new bigint("420"):hex()) --> 1A4

bigint.binary

Provides a binary representation of the Bigint.

pluto
local bigint = require "pluto:bigint"
print(new bigint("420"):binary()) --> 110100100

bigint.add, __add

Adds two bigints together. Returns a new bigint.


bigint.sub, __sub

Performs subtraction on two bigints. Returns a new bigint.


bigint.mul, __mul

Performs multiplication on two bigints. Returns a new bigint.


bigint.div

Performs division on two bigints. Returns two new bigints: quotient and remainder.

pluto
local bigint = require "pluto:bigint"
print(new bigint(10):div(new bigint(3))) --> 3 1

__div

Performs division on two bigints. Returns a new bigint with the quotient.

pluto
local bigint = require "pluto:bigint"
print(new bigint(10) / new bigint(3)) --> 3

bigint.mod, __mod

Performs divison on two bigints. Returns a new bigint with the remainder.

pluto
local bigint = require "pluto:bigint"
print(new bigint(10) % new bigint(3)) --> 1

__unm

Returns a new bigint with the sign bit flipped.

pluto
local bigint = require "pluto:bigint"
print(-new bigint(10)) --> -10

bigint.abs

Returns a new bigint with the sign bit zeroed.

pluto
local bigint = require "pluto:bigint"
print(new bigint(-10):abs()) --> 10

bigint.pow, __pow

Performs expontentiation on two bigints. Returns a new bigint.

pluto
local bigint = require "pluto:bigint"
print(new bigint(2) ^ new bigint(10)) --> 1024

bigint.gcd

Computes the greates common divisor shared by 2 bigints.

pluto
local bigint = require "pluto:bigint"
print(bigint.new(48):gcd(bigint.new(18))) --> 6

bigint.bitlength

Returns the position of the most significant set bit as a plain integer.

pluto
local bigint = require "pluto:bigint"
print(new bigint(420):bitlength()) --> 9

bigint.isprobableprime

Non-deterministically check if a number is prime.

Parameters

  1. The bigint.
  2. Number of Miller-Rabin iterations to perform.
pluto
local bigint = require "pluto:bigint"
print(new bigint(91):isprobableprime(10)) --> false

bigint.export

Exports the bigint to a big-endian binary string.

Parameters

  1. The bigint.
  2. The minimum number of bytes to return.
pluto
local bigint = require "pluto:bigint"
print(new bigint("1056"):export():tohex()) --> 0420
print(new bigint("1056"):export(4):tohex()) --> 00000420

bigint.import

Creates a bigint from a big-endian binary string.

pluto
local bigint = require "pluto:bigint"
print(bigint.import("\x04\x20"):tostring()) --> 1056
print(bigint.import("\x00\x00\x04\x20"):tostring()) --> 1056